package com.wondersgroup.fjzlpt.console.dao.impl.user;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.console.dao.inter.user.UserDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class UserDAOImpl extends BaseJTDAOImpl implements UserDAO{
	@Override
	@Autowired
	@Qualifier("TB_AUTH_USER")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}


	public JSONObject queryPage(Map<String, String> map){
		StringBuilder sbud = new StringBuilder("select * from view_yljgryxx where 1=1 \n");
		List list = new ArrayList();
		String value = map.get("PTJGID");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and PTJGID = ? ");
			list.add(value);
		}
		value = map.get("XM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and XM like ? ");
			list.add("%"+value+"%");
		}
		value = map.get("JSM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and ROLENAME like ? ");
			list.add("%" + value + "%");
		}
		value = map.get("USERNAME");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and USERNAME like ? ");
			list.add("%" + value + "%");
		}
		value = map.get("STATE");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and STATE = ? ");
			list.add(value);
		}
		value = map.get("JGXZQHDM");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append("and JGXZQHDM like ?");
			list.add(value+"%");
		}
		PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by YLJGDM, KSDM, YSGH", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}

	public boolean isExistUsername(String username,String notUserId){
		StringBuilder sbud = new StringBuilder("select count(1) from ")
				.append(this.tb.getTableName())
				.append(" where username = ? ");
				sbud.append("and userid <> ? ");//排除自己
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{username,notUserId});
		return this.isExist(sb);
	}

	public void deleteByUserId(String userId){
		StringBuilder sbud = new StringBuilder("delete from ")
				.append(this.tb.getTableName()).append(" where USERID=?");
		SQLBean sb = new SQLBean(sbud.toString(), new Object[]{userId});
		update(sb);
	}
}
